package com.hhx.androidrxmvp.base.mvp;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/**
 * Created by Administrator on 2019/3/16.
 */

public class BasePresenter<V extends BaseContract.BaseView> implements BaseContract.BasePresenter<V> {

    public static String EXCEPTION_MVP_VIEW_NOT_ATTACHED = "view is not attached!";
    protected Reference<V> mRefView;

    protected V getView() {
        if (!isViewAttached()) {
            throw new IllegalStateException(EXCEPTION_MVP_VIEW_NOT_ATTACHED);
        }
        return mRefView.get();
    }

    protected boolean isViewAttached() {
        return mRefView != null && mRefView.get() != null;
    }

    @Override
    public void attachView(V view) {
        mRefView = new WeakReference<V>(view);
    }

    @Override
    public void detachView() {
        if (mRefView != null) {
            mRefView.clear();
            mRefView = null;
        }
    }
}
